<script src="ext-core.js"></script>
<script src="jquery.min.js"></script>
<script type="text/javascript" src="chrome_ex_oauthsimple.js"></script>
<script type="text/javascript" src="chrome_ex_oauth.js"></script>
<script src="blz.js"></script>
<script src="zoqui.js"></script>
<script>
var calendar = null;

var settings = {
	calendars: null
};

function isLogged(){
	return zoqui.Calendar.isLogged();
};

function login(callback){
	if (!isLogged()){
		zoqui.Calendar.login(callback);
	}
};

function logout(){
	if (isLogged()){
		zoqui.Calendar.logout();
	}
};

function writeEvent(port, calendarTitle, eventTitle, waitTime){
	console.log("getCalendarURI("+calendarTitle+")");
	zoqui.Calendar.getCalendarURI(calendarTitle,function(calendarURI){
		console.log("Calendar URI="+calendarURI);
		zoqui.Calendar.getLastEvent(calendarURI,eventTitle,function(event){
			if (event==null){
				// Keep triing in interval of 100ms, 200ms, 400ms, 800ms, 1600ms (overall searching timeout:3100ms)
				if (waitTime<=1600){
					console.log('Event '+eventTitle+' not found - trying again in ' + parseInt(waitTime*2));
					setTimeout(
						function(){
							console.log("Calendar Updater starting WriteEvent " + eventTitle);
							writeEvent(port, calendarTitle, eventTitle, waitTime*2 );
						},waitTime*2
					);
				}
				else{
					port.postMessage({ action:'writeevent', status:'ERR', msg:'Event not found'});
				}
			}
			else{
				var title = event.title['#text'];
				console.log('Last event '+title+' succesfully loaded');
				event.title['#text']='hi'+title;
				zoqui.Calendar.saveEvent(event,function(ret){
					console.log("Finished");
					port.postMessage({ action:'writeevent', status:'OK', msg:event.title['#text']});
				});
			}
		});
	});
	
	return;
}


function init(){

	chrome.tabs.onUpdated.addListener(onTabUpdated);
	function onTabUpdated(tabId, changeInfo) {

	}
	// For long-lived connections:
	chrome.extension.onConnectExternal.addListener(function(port){
		port.onMessage.addListener(function(msg){
			if (msg.action == "writeevent"){
				if (msg.calendarTitle!=null){
					console.log("Calendar Updater writeevent requested for " + msg.calendarTitle);
					setTimeout(
						function(){						
							if (zoqui.Calendar.isLogged()){
								console.log("Calendar Updater starting WriteEvent " + msg.eventTitle);
								writeEvent(port, msg.calendarTitle, msg.eventTitle,100);
							}
							else{
								zoqui.Calendar.login(function(){
									console.log("Calendar Updater starting WriteEvent " + msg.eventTitle);
									writeEvent(port, msg.calendarTitle, msg.eventTitle,100);
								});
							}
						}
						,100
					);
					return;
				}
				port.postMessage({ action:'writeevent', status:'OK', msg:'Empty calendar title'});
				return;
			};
			port.postMessage({action:'writeevent',status:'ERR', msg: 'Invalid action'});
			return;
		});
	});	
};

init();
</script>